package com.example.paper.mvp.view;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.view.View;

import com.example.mvp.view.BaseActivity;
import com.example.paper.R;
import com.example.paper.mvp.contract.AllContract;
import com.example.paper.mvp.model.AllModel;
import com.example.paper.mvp.presenter.AllPresenter;

import java.util.Timer;
import java.util.TimerTask;

public class StartUpActivity extends BaseActivity<AllPresenter> implements AllContract.AllView {

    private int countdown = 2;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
//        //动态权限
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            requestPermissions(new String[]{
//                    "android.permission.READ_PHONE_STATE",
//                    "android.permission.CALL_PHONE"
//            }, 200);
//        }
//
//
//
////        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
////            return;
////        }
////        String imie = PhoneUtils.getIMEI();
////        //存入
////        Map<String, String> map = new HashMap<>();
////        map.put("imie", imie);
////        //解析
////        String json = new Gson().toJson(map);
////        //请求体
////        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
////        HttpFactory.getInstance().factory(HttpType.SIGNTYPE)
////                .getRetrofit()
////                .create(Api.class)
////                .requestToken(body)
////                .subscribeOn(Schedulers.io())
////                .observeOn(AndroidSchedulers.mainThread())
////                .subscribe(new Observer<ResultBean>() {
////                    @Override
////                    public void onSubscribe(@NonNull Disposable d) {
////
////                    }
////
////                    @Override
////                    public void onNext(@NonNull ResultBean resultBean) {
////                        LogUtils.e("onNext" + resultBean.toString());
////                        ToastUtils.showMsg(resultBean.toString());
////                        SpUtils.saveData("token", resultBean.getValues().token);
////                    }
////
////                    @Override
////                    public void onError(@NonNull Throwable e) {
////                        LogUtils.e("onError" + e.toString());
////                    }
////
////                    @Override
////                    public void onComplete() {
////
////                    }
////                });
//
////        //服务
////        Intent intent = new Intent(this, ServiceHolle.class);
////        startService(intent);
////
//
//
//        //测试Token是否成功
////            @Override
////            public void onClick(View v) {
////                HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
////                        .getRetrofit()
////                        .create(Api.class)
////                        .requestTest()
////                        .subscribeOn(Schedulers.io())
////                        .observeOn(AndroidSchedulers.mainThread())
////                        .subscribe(new Observer<ResultBean>() {
////                            @Override
////                            public void onSubscribe(@NonNull Disposable d) {
////
////                            }
////
////                            @Override
////                            public void onNext(@NonNull ResultBean resultBean) {
////                                LogUtils.e("onNext" + resultBean.toString());
////                            }
////
////                            @Override
////                            public void onError(@NonNull Throwable e) {
////                                LogUtils.e("onError" + e.toString());
////                            }
////
////                            @Override
////                            public void onComplete() {
////
////                            }
////                        });
////            }
//
//    }

    @Override
    public View addStatusView() {
        return findViewById(R.id.minll);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        //动态权限
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            requestPermissions(new String[]{
//                    "android.permission.READ_PHONE_STATE",
//                    "android.permission.CALL_PHONE",
//
//            }, 200);
//        }

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.CALL_PHONE,
            },101);
        }
    }

    @Override
    public void initData() {

        mPresenter = new AllPresenter(new AllModel(), this);
        mPresenter.requestToken();

    }

    @Override
    public void retrursh() {
//跳转经过1.5秒跳转
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                countdown--;
                if (countdown == 0) {
                    Intent intent1 = new Intent(StartUpActivity.this, AllActivity.class);
                    startActivity(intent1);
                    finish();
                }
            }
        }, 0, 2000);
    }
}